Skip to content

Fix PropagateCastOps when a removed Cast feeds the same node input twice - #34221

Open
Qiming Bao (14H034160212) wants to merge 1 commit into
microsoft:mainfrom
14H034160212:fix-propagate-cast-repeated-input
Open

Qiming Bao (14H034160212) wants to merge 1 commit into
microsoft:mainfrom
14H034160212:fix-propagate-cast-repeated-input

Conversation

@14H034160212

Copy link
Copy Markdown

Description

PropagateCastOps throws when a Cast it removes feeds the same consumer more than once, as in Where(condition, C, C).

RemoveCastNodesChain and the multi-child branch of RemoveBackToBackCasts looked up the consumer input with optimizer_utils::IndexOfNodeInput, which returns the first match. They removed and re-added only that edge, while std::replace rewired every matching input def. The edge into the second input was left pointing at the removed Cast. Cleaning it up later then fails:

  • One child Cast, which is the reproduction in the issue: Graph::RemoveEdge ... Argument mismatch when removing edge.
  • The FLOAT Cast has another consumer: Graph::RemoveNode ... Can't remove node ... as it still has output edges.

Both loops now go over every input of the consumer that reads the removed output, and move each edge.

Motivation and Context

Fixes #34192.

Tests

New test GraphTransformationTests.PropagateCastOpsTests_RepeatedConsumerInput builds X(fp16) -> Cast(float) -> Cast(fp16) -> C, Y = Where(true, C, C). A second variant gives the float Cast another consumer, which exercises the multi-child branch. Each variant runs with both strategies at levels 0 to 2 through TestGraphTransformer. The test checks that the fp16 Cast is removed and that both Where data inputs read the graph input.

  • On main, the test fails with the two exceptions above.
  • With this change, it passes.
  • onnxruntime_test_all --gtest_filter='GraphTransformation*:*Optimizer*:*Fusion*:GraphUtils*:*PropagateCast*': 793 tests pass (Linux, CPU, Release).

When a removed Cast's output feeds the same consumer more than once, e.g.
Where(condition, C, C), RemoveCastNodesChain and RemoveBackToBackCasts
removed and re-added the edge of the first matching input only, but
replaced every matching input def. The edge to the second input was left
behind and later failed in Graph::RemoveEdge ("Argument mismatch when
removing edge") or Graph::RemoveNode. Move each matching input edge.

Fixes microsoft#34192
Copilot AI balanced review requested due to automatic review settings November 14, 2026 02:35
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

There are no active review issues for the fix preserving graph connectivity when Cast removal affects multiple inputs of the same consumer.

No open issues 🎉

What changed in this PR

Fixes #34192 by making PropagateCastOps preserve graph connectivity when a removed Cast feeds multiple inputs of the same consumer, such as Where(condition, C, C).

Changes:

  • Rewires every matching consumer input edge in cast-chain removal and the multi-child back-to-back Cast removal path.
  • Adds regression coverage for both removal paths, both propagation strategies, and levels 0–2.
File Description
onnxruntime/​test/​optimizer/​graph_transform_test.cc Adds repeated-consumer-input tests with and without another consumer of the FLOAT Cast.
onnxruntime/​core/​optimizer/​propagate_cast_ops.cc Updates both removal loops to move each affected edge and input definition together.

Review effort: Balanced

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Optimizer] PropagateCastOps throws when Where consumes the same Cast output twice

2 participants